#include "./header/mainwindow.h"
#include "ui_mainwindow.h"
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QFile>
#include <QtSql/QSqlError>
#include <qsqldatabase.h>
#include <QtCore>
#include <QtSql>
#include <QtScript/QScriptEngine>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    nam = new QNetworkAccessManager(this);
    connect(nam,SIGNAL(finished(QNetworkReply*)),this,SLOT(finished(QNetworkReply*)));
}

MainWindow::~MainWindow() {
    delete ui;
}

void MainWindow::getHttpGetData() {
    ui->responseText->setText("Data was set");
    QTextCursor lCursor = ui->urlText->textCursor();
    lCursor.movePosition(QTextCursor::PreviousCharacter,
                             QTextCursor::MoveAnchor,1);
    lCursor.movePosition(QTextCursor::StartOfLine,
                             QTextCursor::KeepAnchor,/*count*/1);
    qDebug() << "Sysout!" << endl;
    //QString url = ui->urlText->toPlainText();
    //qcerr() << "line :" << url << endl;
    //QString baseString = "http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.quotes where symbol in  ('YHOO','AAPL','GOOG','MSFT')&format=xml&env=http://datatables.org//alltables.env";
    QString baseString1 = "http://query.yahooapis.com/v1/public/yql?q=select symbol from yahoo.finance.quotes where symbol in  ('YHOO','AAPL','GOOG','MSFT')&format=json&env=http://datatables.org//alltables.env";
    QString url = baseString1;

    QSqlDatabase defaultDB = QSqlDatabase::addDatabase("QMYSQL3");
    if ( !defaultDB.isValid() ) {
        qWarning( "Failed to connect to the database driver" );
    }
    defaultDB.setDatabaseName( "nicu" );
    defaultDB.setUserName( "root" );
    defaultDB.setPassword( "root" );
    defaultDB.setHostName( "http://localhost:8889" );

    if ( defaultDB.isOpen() ) {
        // Database successfully opened; we can now issue SQL commands.
    }

    //QByteArray postData;
    nam->get(QNetworkRequest(QUrl(url)));
}

void MainWindow::getHttpPostData() {
    ui->urlText->setText("Post Clicked");
}

void MainWindow::finished(QNetworkReply *reply) {
    if(reply->error() == QNetworkReply::NoError) {
        QByteArray result;
        result = reply->readAll();
        ui->responseText->setText(result);
        QScriptValue sc;
        QScriptEngine engine;

        sc = engine.evaluate(result);
        qDebug() << sc.property("query").isArray() << endl;
        qDebug() << sc.property("query").isBool() << endl;
        qDebug() << sc.property("query").isBoolean() << endl;
        qDebug() << sc.property("query").isDate() << endl;
        qDebug() << sc.property("query").isError() << endl;
        qDebug() << sc.property("query").isFunction() << endl;
        qDebug() << sc.property("query").isNull() << endl;
        qDebug() << sc.property("query").isNumber() << endl;
        qDebug() << sc.property("query").isObject() << endl;
        qDebug() << sc.property("query").isQMetaObject() << endl;
        qDebug() << sc.property("query").isQObject() << endl;
        qDebug() << sc.property("query").isRegExp() << endl;
        qDebug() << sc.property("query").isString() << endl;
        qDebug() << sc.property("query").isUndefined() << endl;

        if (sc.property("query").isArray()) {
            qDebug() << "Under the array!" << endl;
                    QStringList items;
                    qScriptValueToSequence(sc.property("result"), items);
                    foreach (QString str, items) {
                         qDebug("value %s",str.toStdString().c_str());
                     }
            }
    }
    else {
        ui->responseText->setText(reply->errorString());
    }
}
